package com.hmall.api.config;

import com.hmall.api.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }

    //注册商品的熔断降级处理类
    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }
    //注册 CartClientFallback
    @Bean
    public CartClientFallback cartClientFallback(){
        return new CartClientFallback();
    }
    // 注册 TradeClientFallback
    @Bean
    public TradeClientFallback tradeClientFallback(){
        return new TradeClientFallback();
    }
    //注册 UserClientFallback
    @Bean
    public UserClientFallback userClientFallback(){
        return new UserClientFallback();
    }


    //    注册feign客户端日志，记录日志
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

//    feign请求拦截器
    @Bean
    public RequestInterceptor feignRequestInterceptor() {
        return new RequestInterceptor() {
            public void apply(RequestTemplate requestTemplate) {
//                获取线程变量中的用户id
                Long userId = UserContext.getUser();
                //设置请求头
                if (userId != null) {
                    requestTemplate.header("user-info", userId.toString());
                }

            }
        };
    }
}
